﻿using MetaView.Shell.Common;
using System.IO;
using System.Runtime.CompilerServices;

namespace MetaView.Shell.Tools.Helper
{
    public class LogHelper
    {
        private string logfile = string.Empty;
        private string content = string.Empty;

        public LogHelper()
        {
            string Path = AppDomain.CurrentDomain.BaseDirectory + @"\Logs\";
            if (Directory.Exists(Path) == false)
            {
                Directory.CreateDirectory(Path);
            }

            logfile = Path + DateTime.Now.ToString("yyyy-MM-dd") + ".log";
        }

        private static LogHelper? istance;
        public static LogHelper Istance
        {
            get
            {
                istance ??= new LogHelper();
                return istance;
            }
        }

        private readonly static object lockObj = new(); //状态锁

        public void Write(string Msg, bool hasTime = true, [CallerMemberName] string memberName = "", [CallerFilePath] string sourceFilePath = "", [CallerLineNumber] int sourceLineNumber = 0)
        {
            #region 写日志

            lock (lockObj)
            {
                using FileStream m_FileStream = new(logfile, FileMode.Append, FileAccess.Write, FileShare.Read);
                using StreamWriter m_StreamWriter = new(m_FileStream);
                content = string.Empty;
                string time = hasTime ? "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]:" : string.Empty;

                string WriteMsg = string.Format("{0}{1}", Msg, Environment.NewLine);
                if (GlobalStaticData.LanguageNum == 0)
                {
                    if (!string.IsNullOrEmpty(sourceFilePath))
                    {
                        content += $"\r\n在 {sourceFilePath} 类中, ";
                    }
                    if (!string.IsNullOrEmpty(memberName))
                    {
                        content += $"\r\n函数名称 => {memberName}, ";
                    }
                    if (sourceLineNumber > 0)
                    {
                        content += $"第 {sourceLineNumber} 行, ";
                    }
                    WriteMsg = $"{time}{content}\r\n内容 => {WriteMsg}";
                    WriteMsg += Environment.NewLine;
                }
                else
                {
                    if (!string.IsNullOrEmpty(sourceFilePath))
                    {
                        content += $"\r\nIn the {sourceFilePath} class, ";
                    }
                    if (!string.IsNullOrEmpty(memberName))
                    {
                        content += $"\r\nfunction name is => {memberName}, ";
                    }
                    if (sourceLineNumber == 0)
                    {
                        content += $"line {sourceLineNumber}, ";
                    }
                    WriteMsg = $"{time}{content}\r\n content is  => \r\n{WriteMsg}";
                    WriteMsg += Environment.NewLine;
                }
                m_StreamWriter.Write(WriteMsg);
                m_StreamWriter.Close();
                m_FileStream.Close();
            }

            #endregion
        }

    }
}
